home *** CD-ROM | disk | FTP | other *** search
- Path: uhura.phoenix.net!usenet
- From: Robert Sanford <roberts@lbms.com>
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
- Subject: Re: Virtual function question
- Date: Tue, 02 Apr 1996 08:38:17 -0600
- Organization: LBMS, Inc
- Message-ID: <31613BD9.5B4D@lbms.com>
- References: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
- NNTP-Posting-Host: 205.241.99.122
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Ross Boylan wrote:
- >
- > I am trying to define, a la Smalltalk, a mix-in which defines the
- > various comparison operators in terms of one fundamental operator, <.
- >
- > typedef int bool;
- > class RBMagnitude {
- > public:
- > //subclass should redefine < with appropriate types
- > virtual bool operator<(const RBMagnitude&) const= 0;
- > virtual bool operator>(const RBMagnitude& x) const {return *this <
- > x;};
- > // and many more
- > };
- >
- > class A : public RBMagnitude {
- > public:
- > virtual bool operator<(const A& a) const {return (this->n < a.n);};
- > private:
- > int n;
- > };
- >
- > class A test;
- >
- > This fails in MSVC++ 4.0 with the error that A is virtual because the
- > operator<(const RBMagnitude&) is not defined.
- > It looks to me as if this is because the operator< for A has a
- > different type signature (it uses an A rather than an RBMagnitude).
- > If so, the problem is the language definition, not Microsoft's
- > implementation.
-
- No, the problem is not with the language definition. The language does what
- it is supposed to. You have declared a different function because of the
- different signatures.
-
- >
- > 1) Is this interpretation correct?YES.
-
- > 2) Is there any way around this problem? Commenting out the
- > definition of RBMagnitude::operator< doesn't work, because
- > RBMagnitude::operator> requires it. I suppose I could define a bogus
- > operator< which throws an exception, but this seems awkward (in
- > particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
- > run time rather than compile time).
- > 1) My personal design perspective is that when you define a virtual function,
- it needs to be valid for all derived classes. You are comparing two
- instances of RBMagnitude with the virtual function definition declared in
- the base class. Why should the results be different for derived classes?
-
- 2) A template function for operator < would work. There are example of this
- in the STL.
-
- > I would appreciate e-mail; I'll summarize here.
-
- oops ;)
-
- --
- --------------------------------------
- / /
- / Robert J. Sanford, Jr. /
- / roberts@lbms.com /
- / /
- / The opinions expressed by me do /
- / not reflect those of my employer. /
- / /
- / /
- -------------------------------------
-